<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class All extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->library('encrypt');
    }

    function store() {
        $this->db->where('nim', $this->input->post('nim'));
        $nama = $this->db->get('mahasiswa');
        if ($nama->num_rows() != 1) {
            $respon = json_decode($this->xmlrpc->display_response());
            $this->nim = $this->input->post('nim');
            $this->nama = $respon->name;
            $this->foto = $respon->foto;
            $this->db->insert('mahasiswa', $this);
        } else {
            $respon = json_decode($this->xmlrpc->display_response());
            $this->nim = $this->input->post('nim');
            $this->nama = $respon->name;
            $this->foto = $respon->foto;
            $this->db->where('nim', $this->input->post('nim'));
            $this->db->update('mahasiswa', $this);
        }
    }

    function nama() {
        $this->db->where('nim', $this->session->userdata('nim'));
        $nama = $this->db->get('mahasiswa');
        if ($nama->num_rows() > 0) {
            return $nama->result_array();
        }
    }

    function kandidat() {
        $kand = $this->db->get('kandidat');
        if ($kand->num_rows() > 0) {
            return $kand->result_array();
        }
    }

    function cekpol() {
        $this->db->where('nim', $this->session->userdata('nim'));
        $nama = $this->db->get('polling');
        if ($nama->num_rows() == 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function polling() {
        $this->db->where('nim', $this->session->userdata('nim'));
        $nama = $this->db->get('polling');
        if ($nama->num_rows() != 1) {
            $pilih = $this->input->post('pilihan');
            $pilihan = $this->security->xss_clean($pilih);
            $this->nim = $this->session->userdata('nim');
            $this->kandidat_id = $pilihan;
            $this->db->insert('polling', $this);
        }
    }

    function pilih() {
        $pilih = $this->db->get('polling');
        if ($pilih->num_rows() > 0) {
            return $pilih->result_array();
        }
    }

    function role_time() {
        $date['today'] = $this->db->query('SELECT CURRENT_DATE();');
        $date['open'] = $this->db->query('SELECT open FROM role_time');
        $date['close'] = $this->db->query('SELECT close FROM role_time');
        return $date;
    }

    function today() {
        $today = $this->db->query('SELECT CURRENT_DATE();');
        if ($today->num_rows() > 0) {
            return $today->result_array();
        }
    }

    function open() {
        $open = $this->db->query('SELECT open FROM role_time');
        if ($open->num_rows() > 0) {
            return $open->result_array();
        }
    }

    function close() {
        $close = $this->db->query('SELECT close FROM role_time');
        if ($close->num_rows() > 0) {
            return $close->result_array();
        }
    }

    /* admin area */

    function login() {
        $data['nim'] = $this->input->post('nim');
        $data['password'] = $this->input->post('password');
        $username = $this->security->xss_clean($data['nim']);
        $password = $this->security->xss_clean($data['password']);
        $this->db->where('nim', $username);
        $this->db->where('password', $this->encrypt->sha1($password));
        $data = $this->db->get('user');
        if ($data->num_rows == 1) {
            return TRUE;
        }
    }

    function rangking() {
        $hasil = $this->db->query('SELECT a.nama_kandidat namkand,COUNT(b.kandidat_id) qty FROM kandidat a LEFT JOIN polling b ON a.id=b.kandidat_id GROUP BY b.kandidat_id ORDER BY COUNT(b.kandidat_id) DESC');
        if ($hasil->num_rows() > 0) {
            return $hasil->result_array();
        }
    }

    function info() {
        $hasil = $this->db->query('SELECT COUNT(nim) jml FROM polling');
        if ($hasil->num_rows() > 0) {
            return $hasil->result_array();
        }
    }

    function profile() {
        $this->db->select('a.nim,b.nama');
        $this->db->from('user a');
        $this->db->join('mahasiswa b', 'a.nim = b.nim');
        $this->db->where('a.nim', $this->session->userdata('nim'));
        $pro = $this->db->get();
        if ($pro->num_rows() > 0) {
            return $pro->result_array();
        }
    }

    function password() {
        $nim = $this->session->userdata('nim');
        $data = $this->input->post('password');
        $pass = $this->security->xss_clean($data);
        $wd = $this->encrypt->sha1($pass);
        $password = array('password' => $wd);
        $this->db->where('nim', $nim);
        $this->db->update('user', $password);
    }

    function showtime() {
        $show = $this->db->get('role_time');
        if ($show->num_rows() > 0) {
            return $show->result_array();
        }
    }

    function settime() {
        $nim = $this->session->userdata('nim');
        $open = $this->input->post('setopen');
        $close = $this->input->post('setclose');
        $set['open'] = $this->security->xss_clean($open);
        $set['close'] = $this->security->xss_clean($close);
        $this->open = $set['open'];
        $this->close = $set['close'];
        $this->by = $nim;
        $this->db->update('role_time', $this);
    }

    function qtyvst() {
        $this->db->select_max('id', 'qtyvst');
        $qtyvst = $this->db->get('visitor');
        if ($qtyvst->num_rows() > 0) {
            return $qtyvst->result_array();
        }
    }

    function lastip() {
        $lastip = $this->db->query('SELECT MAX(id) max, ip_address FROM visitor HAVING max(id)');
        if ($lastip->num_rows() > 0) {
            return $lastip->result_array();
        }
    }

    function online() {
        $qtyonline = $this->db->query('SELECT COUNT(*) qtyol FROM visitor');
        if ($qtyonline->num_rows() > 0) {
            return $qtyonline->result_array();
        }
    }

    function kand1(){
        $res1 = $this->db->query('SELECT nim FROM polling WHERE kandidat_id = 1 ORDER BY nim');
        if($res1->num_rows > 0){
            return $res1->result_array();
        }
    }

    function kand2(){
        $res2 = $this->db->query('SELECT nim FROM polling WHERE kandidat_id = 2 ORDER BY nim');
        if($res2->num_rows > 0){
            return $res2->result_array();
        }
    }

}

/* End of file all.php */
/* Location: ./application/model/all.php */
